package org.alex.sample.web;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.alex.sample.VO.SubjectVOToTest;
import org.alex.sample.VO.UserVO;
import org.alex.sample.model.User;
import org.alex.sample.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
	
	@Autowired
	private StudentService studentService;

	//学生科目列表
	@RequestMapping(value = "/student")
	public String student(Model model, HttpServletRequest request) throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("loginUser");
		UserVO userVO = studentService.findStudentById(user.getId());
		model.addAttribute("stdeunt", userVO);
		return "student/studentSubjectList";
	}
	
	//进入考试并跳到考试页面
	@RequestMapping(value = "/studentExam")
	public String goToExam(Model model, @RequestParam(name = "subId") Integer subId) throws Exception {
		SubjectVOToTest subjectVOToTest = studentService.findAllTestBySubjectId(subId);
		model.addAttribute("subject", subjectVOToTest);
		return "student/studentExam";
	}
	
	//提交试卷保存分数到数据库
	@ResponseBody
	@RequestMapping(value = "/saveExamScore")
	public Map<String, Object> saveExamScore(@RequestParam(name = "score") double score, 
			@RequestParam(name = "subId") Integer subId, HttpServletRequest request) throws Exception{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("loginUser"); //获取到登录的用户
		Integer userId = user.getId(); //获取用户id
		Map<String, Object> messages = studentService.saveExamScore(score, subId, userId);
		return messages;
	}
	
}
